package com.kkb.robotcustomerservice.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kkb.robotcustomerservice.dao.QuestionsDao;
import com.kkb.robotcustomerservice.entities.QuestionTemplate;
import com.kkb.robotcustomerservice.entities.Questions;
import com.kkb.robotcustomerservice.service.QuestionsService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author coffeeee
 */
@Service
public class QuestionsServiceImpl implements QuestionsService {

    @Autowired(required = false)
    private QuestionsDao questionsDao;

    @Override
    public Questions findById(Integer id) {
        return questionsDao.selectById(id);
    }

    @Override
    public Questions findByQuestion(String question) {
        Questions questions = new Questions();
        questions.setAnswer("嘿嘿，我不知道");
        return questions;
    }

    @Override
    public List<Questions> getQuestions() {
        return questionsDao.selectList(Wrappers.query());
    }

    /**
     * 批量删除
     * @return 成功删除的记录数量
     * @author hefuren
     */
    @Override
    public int batchDelete(List<Integer> ids) {
        return questionsDao.deleteBatchIds(ids);
    }

    /**
     * 解析文件
     * @param file 要解析的文件
     * @return 返回Question集合
     * @author hefuren
     */
    @Override
    public List<QuestionTemplate> parseFile(MultipartFile file) throws IOException {
        //获取文件输入流
        InputStream ExcelInputStream = file.getInputStream();
        //文件对象、Sheet对象、Row对象、Cell对象、返回的结果对象
        XSSFWorkbook workbook = new XSSFWorkbook(ExcelInputStream);
        XSSFSheet sheet;
        XSSFRow row;
        XSSFCell cell;
        List<QuestionTemplate> result = new ArrayList<>();

        //sheet的迭代器，遍历sheet
        Iterator<Sheet> sheetIterator = workbook.sheetIterator();
        while (sheetIterator.hasNext()){
            sheet = (XSSFSheet) sheetIterator.next();
            //row的迭代器，遍历每一行
            Iterator<Row> rowIterator = sheet.rowIterator();
            rowIterator.next(); //跳过第一行
            while (rowIterator.hasNext()){
                row = (XSSFRow) rowIterator.next();
                //cell迭代器，用于读取每一行的数据
                Iterator<Cell> cellIterator = row.cellIterator();
                String standardQuestion = cellIterator.next().getStringCellValue();
                String big_cate = cellIterator.next().getStringCellValue();
                String intention = cellIterator.next().getStringCellValue();
                String keywords = cellIterator.next().getStringCellValue();
                String similarQuestion = cellIterator.next().getStringCellValue();
                QuestionTemplate questionTemplate = new QuestionTemplate(standardQuestion, big_cate, intention, keywords, similarQuestion);
                result.add(questionTemplate);
            }
        }
        return result;
    }
}
